package com.fit.WatchShop.pages;

import java.util.Collection;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.WatchShop.beans.PopravkaBean;
import com.fit.WatchShop.interfaces.Popravka;
import com.fit.WatchShop.interfaces.User;
import com.fit.WatchShop.interfaces.Watch;
import com.fit.WatchShop.interfaces.WatchWorker;
import com.fit.WatchShop.service.Popravke;
import com.fit.WatchShop.service.WatchWorkers;
import com.fit.WatchShop.service.Watches;

public class EntryPopravka {
		
		@SessionState
		private User user;

		private boolean userExists;
		
		public User getUser() {
			return user;
		}

		@Persist
		private String popravkaWatchWorker;
		@Persist
		private String popravkaWatch;

		@InjectPage
		private ViewPopravka viewPopravkaPage;
		@InjectPage
		private ViewError viewErrorPage;

		@Inject
		private Watches watches;
		@Inject
		private WatchWorkers watchWorkers;
		@Inject
		private Popravke popravke;

		public Collection<Watch> getWatches() {
			return watches.retrieve();
		}

		public Collection<WatchWorker> getWatchWorkers() {
			return watchWorkers.retrieve();
		}

		@OnEvent(value = "submit", component = "popravkaInputForm")
		Object onFromSubmit() {
			
			
			Watch watch = null;
			WatchWorker watchWorker = null;

			try {
				watchWorker = watchWorkers
						.retrieveSingleWatchWorker(getPopravkaWatchWorker());
				watch = watches.retrieveSingleWatch(getPopravkaWatch());
			} catch (Exception ex) {
				viewErrorPage.setPassedMessage(ex.toString());
				return viewErrorPage;
			}

			if (watchWorker == null) {
				viewErrorPage
						.setPassedMessage("Greska u prepoznavanju watchWorker-a");
				return viewErrorPage;
			}
			if (watch == null) {
				viewErrorPage
						.setPassedMessage("Greska u prepoznavanju watch-a");
				return viewErrorPage;
			}

			Popravka poprvkaBean = new PopravkaBean();
			
			this.popravkaWatch = "";
			this.popravkaWatchWorker = "";
			
			return viewPopravkaPage;
		}

		public String getPopravkaWatchWorker() {
			return popravkaWatchWorker;
		}

		public void setPopravkaWatchWorker(String popravkaWatchWorker) {
			this.popravkaWatchWorker = popravkaWatchWorker;
		}

		public String getPopravkaWatch() {
			return popravkaWatch;
		}

		public void setPopravkaWatch(String popravkaWatch) {
			this.popravkaWatch = popravkaWatch;
		}




	}

